@using Grand.Business.Core.Interfaces.Catalog.Products
@using Grand.Business.Core.Interfaces.Checkout.GiftVouchers
@using Grand.Business.Core.Interfaces.Checkout.Payments
@using Grand.Business.Core.Interfaces.Common.Directory
@using Grand.Business.Core.Interfaces.Common.Localization
@using Grand.Business.Core.Interfaces.Common.Stores
@using Grand.Business.Core.Interfaces.Storage
@model (IList<Order>, string)
@inject IProductService _productService
@inject ICountryService _countryService
@inject IStoreService _storeService
@inject IPictureService _pictureService
@inject PdfSettings _pdfSettings
@inject ITranslationService _translationService
@inject IPaymentService _paymentService
@inject ICurrencyService _currencyService
@inject IGiftVoucherService _giftVoucherService
@{
    var logoPicture = await _pictureService.GetPictureById(_pdfSettings.LogoPictureId);
    var logoExists = logoPicture != null;
    var limitbyVendor = !string.IsNullOrEmpty(Model.Item2);
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .summary {
            padding-top:20px;
        }

        .footer {
            position: fixed;
            bottom: 0;
            width: 100%;
        }


        /* https://www.w3schools.com/html/html_table_borders.asp */
      table.CodeTable {
              width: 100%;  
      }
      .CodeTable .line {border-bottom:1px solid #EEE; border-top: 1px solid #EEE; border-left: 1px solid #FFFFFF; border-right: 1px solid #FFFFFF;}
      table.CodeTable th,
      table.CodeTable td {
        /* width: 100%; */
        /*border: 0px solid black;*/
        border: none;
        border-collapse: collapse;
      }
      .Header {
        background-color: grey;
        color: white;
        padding: 5px;
      }

      table.Table {
        width: 100%;
      }
      table.Table,
      table.Table th,
      table.Table td {
        /*white-space: nowrap;*/
        text-align: left;
        /* border: 1px solid black; */
        border-collapse: collapse;
      }
      table.Table th,
      table.Table td {
        padding: 12px 8px 3px;
        text-align: right;
      }
      table.Table th.leftalign,
      table.Table td.leftalign {
        text-align: left;
      }
      table.Table th {
        padding-top: 5px;
        padding-bottom: 5px;
        border-bottom: 1px solid black; 
      }
      table.Table th.first,
      table.Table td.first {
        text-align: left;
        width: 30px;
      }
      table.Table th.autoWidth,
      table.Table td.autoWidth {
        width: 400px;
      }
      table.Table th.detailText,
      table.Table td.detailText {
        font-size: 10px;
        padding-top: 3px;
        padding-bottom: 2px;
      }
      .bold {
        font-weight: bold;
      }
      table.Table tfoot {
        padding-top: 4px;
        border-top: 1px solid black;
        border-bottom: 1px solid black;
      }
      table.Table tfoot tr,
      table.Table tfoot td {
        padding-top: 4px;
        padding-bottom: 4px;
      }

    </style>
</head>
<body>
<main style="padding:10pt; font-size:14px">
    @{
        var i = 0;
    }
    @foreach (var order in Model.Item1)
    {
        var priceIncludesTax = order.CustomerTaxDisplayTypeId == TaxDisplayType.IncludingTax;
        var store = await _storeService.GetStoreById(order.StoreId);
        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceHeaderText))
        {
            <div>@Html.Raw(_pdfSettings.InvoiceHeaderText)</div>
        }

        <h4>@store.CompanyName</h4>
        if (logoExists)
        {
            var url = await _pictureService.GetPictureUrl(logoPicture, 0, false, store.SslEnabled ? store.SecureUrl : store.Url);
            <div style="float: right; top:10px;">
                <img src="@url" title="logo" alt="logo"/>
            </div>
        }

        <h6>@(string.Format(_translationService.GetResource("PDFInvoice.Order#", order.CustomerLanguageId), order.OrderNumber)) (@order.CreatedOnUtc)</h6>
        <table class="CodeTable">
            <tr>
                <td class="line">
                    @if (order.BillingAddress != null)
                    {
                        <b>@(_translationService.GetResource("PDFInvoice.BillingInformation", order.CustomerLanguageId))</b>
                        <br/>
                        @if (!string.IsNullOrEmpty(order.BillingAddress.Company))
                        {
                            @(string.Format(_translationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.BillingAddress.Company))<br/>
                        }

                        @(string.Format(_translationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.BillingAddress.FirstName + " " + order.BillingAddress.LastName))<br/>
                        @(string.Format(_translationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.BillingAddress.PhoneNumber))<br/>
                        @(string.Format(_translationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.BillingAddress.Address1))<br/>
                        @(order.BillingAddress.City + ", " + order.BillingAddress.ZipPostalCode)<br/>
                        @((await _countryService.GetCountryById(order.BillingAddress.CountryId))?.Name)<br/>
                        <br/>
                        var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
                        var paymentMethodStr = paymentMethod != null ? paymentMethod.FriendlyName : order.PaymentMethodSystemName;
                        <b>@(string.Format(_translationService.GetResource("PDFInvoice.PaymentMethod", order.CustomerLanguageId), paymentMethodStr))</b>
                        <br/>
                        @await Component.InvokeAsync("Widget", new { widgetZone = "pdf_payment_method_below", additionalData = order.Id })
                    }
                </td>
                <td class="line">
                    @if (order.ShippingAddress != null)
                    {
                        <b>@(_translationService.GetResource("PDFInvoice.ShippingInformation", order.CustomerLanguageId))</b>
                        <br/>
                        @if (!string.IsNullOrEmpty(order.ShippingAddress.Company))
                        {
                            @(string.Format(_translationService.GetResource("PDFInvoice.Company", order.CustomerLanguageId), order.ShippingAddress.Company))<br/>
                        }

                        @(string.Format(_translationService.GetResource("PDFInvoice.Name", order.CustomerLanguageId), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName))<br/>
                        @(string.Format(_translationService.GetResource("PDFInvoice.Phone", order.CustomerLanguageId), order.ShippingAddress.PhoneNumber))<br/>
                        @(string.Format(_translationService.GetResource("PDFInvoice.Address", order.CustomerLanguageId), order.ShippingAddress.Address1))<br/>
                        @(order.ShippingAddress.City + ", " + order.ShippingAddress.ZipPostalCode)<br/>
                        @((await _countryService.GetCountryById(order.ShippingAddress.CountryId))?.Name)<br/>
                        <br/>
                        <b>@(string.Format(_translationService.GetResource("PDFInvoice.ShippingMethod", order.CustomerLanguageId), order.ShippingMethod))</b>
                        <br/>
                    }
                </td>
            </tr>
        </table>

        <div class="summary">
            @(_translationService.GetResource("PDFInvoice.Product(s)", order.CustomerLanguageId))
        </div>
        <table class="Table">
            <tr>
                <td class="Header leftalign" colspan="2">@(_translationService.GetResource("PDFInvoice.ProductName", order.CustomerLanguageId))</td>
                <td class="Header">@(_translationService.GetResource("PDFInvoice.ProductPrice", order.CustomerLanguageId))</td>
                <td class="Header">@(_translationService.GetResource("PDFInvoice.ProductQuantity", order.CustomerLanguageId))</td>
                <td class="Header">@(_translationService.GetResource("PDFInvoice.ProductTotal", order.CustomerLanguageId))</td>
            </tr>
            @foreach (var item in order.OrderItems.Where(x => x.VendorId == Model.Item2 || !limitbyVendor).ToList())
            {
                <tr>
                    <td class="autoWidth leftalign" colspan="2">
                        @((await _productService.GetProductByIdIncludeArch(item.ProductId)).Name)
                        @if (!string.IsNullOrEmpty(item.AttributeDescription))
                        {
                            <br/>
                            @(Html.Raw(item.AttributeDescription))
                        }
                    </td>
                    <td>
                        @if (priceIncludesTax)
                        {
                            @item.UnitPriceInclTax.ToString("N2")
                        }
                        else
                        {
                            @item.UnitPriceExclTax.ToString("N2")
                        }
                    </td>
                    <td>@item.Quantity</td>
                    <td class="bold">
                        @if (priceIncludesTax)
                        {
                            @item.PriceInclTax.ToString("N2")
                        }
                        else
                        {
                            @item.PriceExclTax.ToString("N2")
                        }
                    </td>
                </tr>
            }
        </table>
        @if (!limitbyVendor)
        {
            <div class="summary">
                @if (!string.IsNullOrEmpty(order.CheckoutAttributeDescription))
                {
                    @(Html.Raw(order.CheckoutAttributeDescription))
                }
                <div style="float: right">

                    @(_translationService.GetResource("PDFInvoice.Sub-Total", order.CustomerLanguageId))
                    @if (priceIncludesTax)
                    {
                        @order.OrderSubtotalInclTax.ToString("N2")
                    }
                    else
                    {
                        @order.OrderSubtotalExclTax.ToString("N2")
                    }
                    @order.CustomerCurrencyCode <br/>
                    @if (order.OrderSubTotalDiscountInclTax > 0 || order.OrderSubTotalDiscountExclTax > 0)
                    {
                        @(_translationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId)) @if (priceIncludesTax)
                        {
                            @order.OrderSubTotalDiscountInclTax.ToString("N2")
                        }
                        else
                        {
                            @order.OrderSubTotalDiscountExclTax.ToString("N2")
                        }

                        @order.CustomerCurrencyCode
                    }
                    @if (order.PaymentMethodAdditionalFeeInclTax > 0 || order.PaymentMethodAdditionalFeeExclTax > 0)
                    {
                        @(_translationService.GetResource("PDFInvoice.PaymentmethodAdditionalFee", order.CustomerLanguageId)) @if (priceIncludesTax)
                        {
                            @order.PaymentMethodAdditionalFeeInclTax.ToString("N2")
                        }
                        else
                        {
                            @order.PaymentMethodAdditionalFeeExclTax.ToString("N2")
                        }

                        @order.CustomerCurrencyCode<br/>
                    }
                    @(_translationService.GetResource("PDFInvoice.Shipping", order.CustomerLanguageId))
                    @if (priceIncludesTax)
                    {
                        @order.OrderShippingInclTax.ToString("N2")
                    }
                    else
                    {
                        @order.OrderShippingExclTax.ToString("N2")
                    }
                    @order.CustomerCurrencyCode<br/>
                    @if (order.OrderDiscount > 0)
                    {
                        @(_translationService.GetResource("PDFInvoice.Discount", order.CustomerLanguageId))
                        @order.OrderDiscount.ToString("N2")
                        @order.CustomerCurrencyCode<br/>
                    }
                    @foreach (var gcuh in await _giftVoucherService.GetAllGiftVoucherUsageHistory(order.Id))
                    {
                        if (gcuh.UsedValue > 0)
                        {
                            var giftvoucher = await _giftVoucherService.GetGiftVoucherById(gcuh.GiftVoucherId);
                            @(string.Format(_translationService.GetResource("PDFInvoice.GiftVoucherInfo", order.CustomerLanguageId), giftvoucher.Code))
                            @gcuh.UsedValue<br/>
                        }
                    }
                    @if (order.OrderTax > 0)
                    {
                        @(_translationService.GetResource("PDFInvoice.Tax", order.CustomerLanguageId))
                        @order.OrderTax.ToString("N2")
                        @order.CustomerCurrencyCode<br/>
                    }
                    <b>
                        @(_translationService.GetResource("PDFInvoice.OrderTotal", order.CustomerLanguageId)) @order.OrderTotal.ToString("N2") @order.CustomerCurrencyCode<br/>
                    </b>

                </div>
            </div>
        }

        if (!string.IsNullOrEmpty(_pdfSettings.InvoiceFooterText))
        {
            <footer class="footer">
                @Html.Raw(_pdfSettings.InvoiceFooterText)
            </footer>
        }

        if (Model.Item1.Count - 1 > i)
        {
            <div style="display:block; clear:both; page-break-after:always;"></div>
        }

        i++;
    }
</main>
</body>
</html>